<?php

namespace Platform\Models\Admin;

use Platform\Models\BaseModel;

class AdminUser extends BaseModel {

    const TABLE_NAME = 'admin_user';

    public function initialize() {
        parent::initialize();
        $this->pk = "adminId";
        $this->setSource(self::TABLE_NAME);
    }

    private $adminId;
    private $username;
    private $roleId;
    private $realName;
    private $password;
    private $loginCount;
    private $loginTime;
    private $loginIp;
    private $createTime;
    private $updateTime;
    private $createUser;
    private $updateUser;
    private $isDel;
    private $img;

    public function columnMap() {
        //列名 =》 属性名
        return [
            'admin_id' => 'adminId',
            'username' => 'username',
            'role_id' => 'roleId',
            'real_name' => 'realName',
            'password' => 'password',
            'login_count' => 'loginCount',
            'login_time' => 'loginTime',
            'login_ip' => 'loginIp',
            'create_time' => 'createTime',
            'update_time' => 'updateTime',
            'create_user' => 'createUser',
            'update_user' => 'updateUser',
            'is_del' => 'isDel',
            'img' => 'img',
        ];
    }

    public function getAdminId() {
        return $this->adminId;
    }

    public function getUsername() {
        return $this->username;
    }

    public function getRoleId() {
        return $this->roleId;
    }

    public function getRealName() {
        return $this->realName;
    }

    public function getPassword() {
        return $this->password;
    }

    public function getLoginCount() {
        return $this->loginCount;
    }

    public function getLoginTime() {
        return $this->loginTime;
    }

    public function getLoginIp() {
        return $this->loginIp;
    }

    public function getCreateTime() {
        return $this->createTime;
    }

    public function getUpdateTime() {
        return $this->updateTime;
    }

    public function getCreateUser() {
        return $this->createUser;
    }

    public function getUpdateUser() {
        return $this->updateUser;
    }

    public function getIsDel() {
        return $this->isDel;
    }

    public function getImg() {
        return $this->img;
    }

    public function setAdminId($adminId) {
        $this->adminId = $adminId;
    }

    public function setUsername($username) {
        $this->username = $username;
    }

    public function setRoleId($roleId) {
        $this->roleId = $roleId;
    }

    public function setRealName($realName) {
        $this->realName = $realName;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

    public function setLoginCount($loginCount) {
        $this->loginCount = $loginCount;
    }

    public function setLoginTime($loginTime) {
        $this->loginTime = $loginTime;
    }

    public function setLoginIp($loginIp) {
        $this->loginIp = $loginIp;
    }

    public function setCreateTime($createTime) {
        $this->createTime = $createTime;
    }

    public function setUpdateTime($updateTime) {
        $this->updateTime = $updateTime;
    }

    public function setCreateUser($createUser) {
        $this->createUser = $createUser;
    }

    public function setUpdateUser($updateUser) {
        $this->updateUser = $updateUser;
    }

    public function setIsDel($isDel) {
        $this->isDel = $isDel;
    }

    public function setImg($img) {
        $this->img = $img;
    }

    /**
     * 根据username获取管理员信息
     * @param type $phone
     */
    public function getAdminUserByUsername($username) {
        $user = parent::findFirst([
                    "conditions" => "username  = :username:",
                    "bind" => [
                        "username" => $username
                    ],
        ]);
        if ($user) {
            $user = $user->toArray();
        }
        return $user;
    }

    /**
     * 根据adminId获取管理员信息
     * @param $adminId
     * @return mixed
     */
    public function getAdminUserById($adminId){
        $user = parent::findFirst([
            "conditions" => "adminId = :adminId:",
            "bind" => [
                "adminId" => $adminId
            ],
        ]);
        if ($user) {
            $user = $user->toArray();
        }
        return $user;
    }

    /**
     * 修改管理员登录信息
     * @param type $userId
     */
    public function updateAdminUserLoginInfo($adminId) {
        $lastLoginIp = get_client_ip();
        $lastLoginTime = time();

        $phql = "UPDATE " . __CLASS__ . " SET loginCount = loginCount + 1 , loginTime = :loginTime: , loginIp = :loginIp: WHERE adminId = :adminId:";
        $result = $this->getModelsManager()->executeQuery($phql, array(
            'loginIp' => $lastLoginIp,
            'loginTime' => $lastLoginTime,
            'adminId' => $adminId,
        ));

        if (!$result) {
            E('更新失败');
        }
    }

    /**
     * 添加管理员
     * @param $username
     * @param $password
     * @param $roleId
     * @param $realName
     */
    public function addAdminUser($username, $password, $roleId, $realName,$img,$adminId){
        $this->username = $username;
        $this->password = $password;
        $this->roleId = $roleId;
        $this->createUser = $adminId;
        $this->updateUser = $adminId;
        $this->realName = $realName;
        $this->img = $img;
        $this->createTime = time();
        $this->updateTime = time();
        $this->save();
    }

    /**
     * 获取所有的管理员信息
     * @return mixed
     */
    public function getAdminUserAll(){
        $adminUserAll = parent::find();
        return $adminUserAll->toArray();
    }

    /**
     * 通过用户ID，角色ID、真实姓名搜索
     * @param $userId
     * @param $roleId
     * @param $realName
     */
    public function search($adminId,$roleId,$realName,$returnObject = false){

        $conditions = "";
        $bind = array();
        if(!empty($adminId)){
            if($conditions){
                $conditions .= " AND ";
            }
            $conditions .= 'adminId = :adminId:';
            $bind['adminId'] = $adminId;
        }

        if(!empty($roleId)){
            if($conditions){
                $conditions .= " AND ";
            }
            $conditions .= 'roleId = :roleId:';
            $bind['roleId'] = $roleId;
        }

        if(!empty($realName)){
            if($conditions){
                $conditions .= " AND ";
            }
            $conditions .= 'realName like :realName:';
            $bind['realName'] = "%".$realName."%";
        }

        $list = parent::find([
            'conditions' => $conditions,
            'bind' => $bind,
        ]);

        //是否返回对象
        if($list && $returnObject == false){
            return $list->toArray();
        }

        return $list;
    }


    /**
     * 修改用户密码
     * @param $adminUser
     */
    public function updateUserInfo($adminId,$newPassword){
        $updateTime = time();
        $phql = "UPDATE " . __CLASS__ . " SET password = :password: , updateTime = :updateTime: WHERE adminId = :adminId:";
        $result = $this->getModelsManager()->executeQuery($phql, array(
            'password' => $newPassword,
            'updateTime' => $updateTime,
            'adminId' => $adminId,
        ));
        if(!$result){
            E("修改失败");
        }
    }

    /**
     * 修改用户头像
     * @param $img
     */
    public function updateImgById($adminId,$img){
        $phql = "UPDATE " . __CLASS__ . " SET img = :img: , createTime = :createTime: WHERE adminId = :adminId:";
        $result = $this->getModelsManager()->executeQuery($phql, array(
            'img' => $img,
            'createTime' => time(),
            'adminId' => $adminId,
        ));

        if (!$result) {
            E('更新失败');
        }
    }



}
